package org.seedltd.upms.mapper.sys;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.base.IBaseMapper;
import org.seedltd.upms.model.sys.entity.SysUser;
import org.seedltd.upms.model.sys.form.SysUserSearchForm;
import org.seedltd.upms.model.sys.vo.SysUserVO;

public interface SysUserMapper extends IBaseMapper<SysUser> {

    BasePage<SysUserVO> selectSysUserList(BasePage<SysUserVO> page, @Param("ew") SysUserSearchForm sysUserSearchForm);

    @Select("select 1 from sys_user where login_name = #{loginName}")
    Integer existsByLoginName(String loginName);

    /**
     * 插入用户和角色关联关系
     * 
     * @param userId
     *            用户主键
     * @param roleIdList
     *            角色主键集合
     * @return
     */
    int insertUserRole(@Param("userId") Long userId, @Param("roleIdList") List<Long> roleIdList);

    /**
     * 删除用户和角色的关联关系
     * 
     * @param userId
     *            用户主键
     * @return
     */
    @Delete("delete from sys_role_user where user_id = #{userId}")
    int deleteUserRoleByUserId(Serializable userId);

    /**
     * 获取用户的角色主键集合
     * 
     * @param userId
     *            用户主键
     * @return
     */
    @Select("select role_id from sys_role_user where user_id = #{userId}")
    Set<Long> listUserRoleByUserId(Long userId);

    /**
     * 获取用户的角色代码集合
     * 
     * @param userId
     *            用户主键
     * @return
     */
    Set<String> listRoleCodeByUserId(@Param("userId") Long userId);

    /**
     * 获取角色集合获取权限代码集合
     * 
     * @param roleCodeList
     *            角色集合
     * @return
     */
    Set<String> listPermissionByRoleCodeList(@Param("roleCodeList") Set<String> roleCodeList);

    /**
     * 插入用户和部门关联关系
     * 
     * @param userId
     *            用户主键
     * @param deptIdList
     *            部门主键集合
     * @return
     */
    int insertUserDept(@Param("userId") Long userId, @Param("deptIdList") List<Long> deptIdList);

    /**
     * 删除用户和部门的关联关系
     * 
     * @param userId
     *            用户主键
     * @return
     */
    @Delete("delete from sys_user_dept where user_id = #{userId}")
    int deleteUserDeptByUserId(Serializable userId);

    /**
     * 获取用户的部门主键集合
     * 
     * @param userId
     *            用户主键
     * @return
     */
    @Select("select dept_id from sys_user_dept where user_id = #{userId}")
    Set<Long> listUserDeptByUserId(Long userId);

    /**
     * 删除用户和岗位的关联关系
     * 
     * @param userId
     *            用户主键
     * @return
     */
    @Delete("delete from sys_user_post where user_id = #{userId}")
    int deleteUserPostByUserId(Serializable userId);
}
